package 模拟实现购物车搭建;

/**
 *  面对对象购物车训练：购物车模块模拟
 */
import java.util.Scanner;
public class ShoppingCart {

    //计算当前购物车已经添加的数量
    public static int conut = 0;
    public static void main(String[] args) {

        // 1. 定义商品类，用来后期创建商品对象
        // 2. 定义商品购物车（容器），现阶段使用数组对象表示。
        Goods[] shopcar = new Goods[100];

        Scanner sc = new Scanner(System.in);
        while(true){

            // 3. 定义一个菜单类 如对商品的增删查改
            Menu.menu();

            String command = sc.next();

            //判断是什么命令
            switch(command){

                case "add":
                    //添加商品到购物车
                    addGoods(shopcar, sc);
                    break;
                case "query":
                    //查询商品到购物车
                    queryGoods(shopcar);
                    break;
                case "update":
                    //修改商品购买数量
                    upadteGoods(shopcar, sc);
                    break;
                case "pay":
                    //结算购买商品的金额
                    payGoods(shopcar);
                    break;
                case "Del":
                    //删除商品
                    delecomm(shopcar, sc);
                    break;
                default :
                    System.out.println("输入的命名错误，请重新输入");
                    break;
            }
        }


    }
        //结算购买商品的金额
    private static void payGoods(Goods[] shopcar) {
        double sum = 0;
        for (int i = 0; i < conut; i++) {
            sum += shopcar[i].price * shopcar[i].quan;
        }
        System.out.println("你需要付款：" + sum + "元");
    }

    //修改商品购买数量
    private static void upadteGoods(Goods[] shopcar, Scanner sc) {
        //让用户输出id，根据ad修改商品的购买数量
        System.out.print("请输入你要修改的商品id：");
            int id = sc.nextInt();
        for (int i = 0; i < conut; i++) {

            if(shopcar[i].id == id){
                System.out.print("输出你要修改的数量: ");
                int quan = 0;
                quan = sc.nextInt();
                shopcar[i].quan = quan;
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("你输入的编号不存在");
        return;
    }

          //查询商品到购物车
    private static void queryGoods(Goods[] shopcar) {

        System.out.printf("%-5s%-5s%-4s%-5s", "编号", "名称", "价格", "数量");
        System.out.println();
        for (int i = 0; i < conut; i++) {
            System.out.printf("%-6d", shopcar[i].id );
            System.out.printf("%-6s", shopcar[i].name );
            System.out.printf("%-6.0f", shopcar[i].price);
            System.out.printf("%-6d\n", shopcar[i].quan);

        }
    }
         //添加商品到购物车
    private static void addGoods(Goods[] shopcar, Scanner sc) {
        //1. 录入用户输入的商品信息
        System.out.print("请输出你购买商品的编号（不重复）：");
        int id = sc.nextInt();
        System.out.print("请输出你购买商品的名称：");
        String name = sc.next();//遇到空格结束

        System.out.print("请输出你购买商品的价格：");
        double price = sc.nextDouble();
        System.out.print("请输出你购买商品的数量：");
        int quan = sc.nextInt();

        Goods g = new Goods();
         g.id = id;
         g.name = name;
         g.price = price;
         g.quan = quan;
        shopcar[conut] = g;

        conut++;
        System.out.println("添加商品成功");
    }

    //删除商品
    public static void delecomm(Goods[] shopcar, Scanner sc){

        System.out.print("请输出你要删除商品的编号：");
        int id = sc.nextInt();

        for (int i = 0; i < conut; i++) {

            if(shopcar[i].id == id){
                for (int j = i; j < conut - 1; j++) {
                    shopcar[j] = shopcar[j + 1];
                }
                conut--;
                System.out.println("删除成功！");
                return;
            }
        }
        System.out.println("没有该商品！");
    }
}
